/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xpstem.lelink.bluetooth.BtDeviceInfo;

import java.util.List;

/**
 * @author billy zhang
 */
public class DeviceListAdapter extends RecyclerView.Adapter {

    public interface OnItemClickListener {
        void onItemClick(DeviceListAdapter adapter, View view, int position);
     }

     private OnItemClickListener onItemClickListener;

     public void setOnItemClickListener(OnItemClickListener itemClickListener) {
         this.onItemClickListener = itemClickListener;
     }

    private static final String TAG = DeviceListAdapter.class.getSimpleName();

    private List<BtDeviceInfo> list;

    public DeviceListAdapter(@Nullable List<BtDeviceInfo> data) {
        this.list = data;
    }

    @Override
     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
         View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.btdevice_item, null);
         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
         view.setLayoutParams(lp);
         return new DeviceViewHolder(this, view);
     }

     @Override
     public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

         DeviceViewHolder holder = (DeviceViewHolder) viewHolder;
         holder.position = position;
         BtDeviceInfo result = list.get(position);
         holder.nameText.setText(result.getName());
         holder.macText.setText(result.getAddress());
         holder.rssiText.setText(String.format("Rssi: %d", result.getRssi()));

     }

     @Override
     public int getItemCount() {
        return list.size();
     }

    class DeviceViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
     {
         public View rootView;
         public TextView nameText;
         public TextView macText;
         public TextView rssiText;

         private DeviceListAdapter adapter;
         public int position;

         public DeviceViewHolder(DeviceListAdapter adapter, View itemView) {
             super(itemView);
             this.adapter = adapter;
             nameText = (TextView) itemView.findViewById(R.id.name);
             macText = (TextView) itemView.findViewById(R.id.mac);
             rssiText = (TextView) itemView.findViewById(R.id.rssi);

             rootView = itemView.findViewById(R.id.device_item);
             rootView.setOnClickListener(this);
         }

         @Override
         public void onClick(View v) {
             if (null != onItemClickListener) {
                 onItemClickListener.onItemClick(adapter, itemView, position);
             }
         }
     }

}
